package com.ag.lwc;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.ag.bean.TbUserDept;
import com.ag.dao.TbDeptDAO;

/**
 * 修改部门id的测试类,部门编码大改版
 * @author user
 *
 */
public class UpdateDeptId {

	/**
	 * 
	 */
	public void updateOneDeptId(String oldId,String newId)
	{
		ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{ "classpath:/applicationContext.xml",
				"classpath:/applicationContext-dgm.xml", 
				"classpath:/applicationContext-hmk.xml",
				"classpath:/applicationContext-lzw.xml",
				"classpath:/applicationContext-lwc.xml",
				"classpath:/applicationContext-lzm.xml",
				"classpath:/applicationContext-wsq.xml",
				"classpath:/applicationContext-wzp.xml",
				"classpath:/applicationContext-zhh.xml",
				"classpath:/applicationContext-zlw.xml",
				"classpath:/applicationContext-zrt.xml",});
		TbDeptDAO deptdao = (TbDeptDAO) context.getBean("TbDeptDAO");
		Session session = deptdao.getSessionFactory().openSession();
		
		Transaction tx = null;
		//开事务更改部门id
		try 
		{
			tx=session.beginTransaction();
			//更改部门id
			TbUserDept dept = (TbUserDept) session.get(TbUserDept.class, oldId);
			dept.setUdId(newId);
			
			tx.commit();
		} 
		catch (HibernateException e) 
		{
			tx.rollback();
			e.printStackTrace();
		}
		session.close();
	}
	public static void main(String[] args)
	{
	
		UpdateDeptId updateDeptId = new UpdateDeptId();
		updateDeptId.updateOneDeptId("CR0113050000", "CR0000100100");
	}

}
